import os
import datetime

import django
from django.http.request import HttpRequest
from django.shortcuts import render
from django.template.loader import get_template
from django.template import Template, Context

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()

# t = Template('My name is {{ name }}.')
# c = Context({'name': 'Nige'})
# print(t.render(c))
# c = Context({'name': 'Barry'})
# print(t.render(c))

# base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# html_file = os.path.join(base_dir, 'templates', 'demo.html')
# fp = open(html_file, 'r', encoding='utf-8')
# t = Template(fp.read())
# fp.close()
#
# context_dict = {'person_name': 'Ten', 'company': '动脑学院',
#                 'item_list': ['Python Web开发', 'Python自动化运维'],
#                 'ship_date': datetime.date(2018, 5, 14),
#                 'ordered_warranty': False}
# c = Context(context_dict)
# html = t.render(c)
# print(html)

# context_dict = {'person_name': 'Ten', 'company': '动脑学院',
#                 'item_list': ['Python Web开发', 'Python自动化运维'],
#                 'ship_date': datetime.date(2018, 5, 14),
#                 'ordered_warranty': False}
# t = get_template('demo.html')
# html = t.render(context_dict)
# print(html)

context_dict = {'person_name': 'Ten', 'company': '动脑学院',
                'item_list': ['Python Web开发', 'Python自动化运维'],
                'ship_date': datetime.date(2018, 5, 14),
                'ordered_warranty': False}
print(render(None, 'demo.html', context_dict).content.decode())

